<div class="filter">
    <div class="item" style="line-height: 30px; padding-left: 10px;">请选择游戏</div>
    <div class="f1 item">
        <select data-am-selected>
        </select>
    </div>
    <div class="clear"></div>
</div>

<table class="form-table-without-line">
    <tr>
        <th>单期限额</th>
        <td>
            <input type="number" class="am-form-field game-max" placeholder="最大限额">
            <button type="button" class="btn-issue-limit">保存</button>
            <div class="clue">说明：不填或者填写0表示不限制，下同。</div>
        </td>
    </tr>
    <tr>
        <th>最大注单数</th>
        <td>
            <input type="number" class="am-form-field bet-count-max" placeholder="最大注单数">
            <button type="button" class="btn-count-limit">保存</button>
        </td>
    </tr>

    <tr>
        <th>单注限额(快速设置)</th>
        <td>
            <input type="number" class="am-form-field quick-min" placeholder="最小限额">
            <input type="number" class="am-form-field quick-max" placeholder="最大限额">
            <button type="button" class="btn-quick">快速设置</button>
        </td>
    </tr>
</table>

<div class="game-detail">
</div>

<script>
    // 填入游戏
    var games = get_game_list();
    var s = $(".f1 select");
    for (var i in games) {
        s.append('<option value="' + games[i] + '">' + get_game_display_name(games[i]) + "</option>");
    }

    var loadGameF = function() {
        var game = s.val();

        // 载入相关的游戏设置页面
        switch_page("admin/limit/" + game + "_limit", $(".game-detail"), function() {
            // 关注tab的点击效果
            var arr = $(".am-tabs-nav li");
            var register_tab_index = function(tab, index) {
                tab.click(function() {
                    $('.am-tabs').tabs('open', index);
                })
            }
            for (var i = 0; i < arr.length; i++) register_tab_index($(arr[i]), i);

            AdminLimit.init(game, Util.getGameRule(game));
        });

        Classify.query("bet.count", game, false, '', function(v) {
            $(".bet-count-max").val(v);
        });
    };
    loadGameF();

    s.change(function() {
        loadGameF();
    });

    function setAllLimit(v, isMin) {
        // 取得当前选中的tab标签
        var arr = $(".am-tabs-bd .am-tab-panel");
        for (var i = 0; i < arr.length; i++) {
            var tab = $(arr[i]);
            if (tab.attr("class").indexOf("am-active") != -1) {
                var inputs = tab.find(isMin ? "input.min" : "input.max");
                inputs.val(v);
            }
        }
    }

    $(".btn-quick").click(function() {
        setAllLimit($(".quick-min").val(), true);
        setAllLimit($(".quick-max").val(), false);
    });
    $(".btn-issue-limit").click(function() {
        var v = $(".game-max").val().trim();
        if (v == '') {
            alert2("请填入最大单期限额的金额（单位：元）！", true);
            return;
        }

        v = parseInt(v);
        if (v < 0) {
            alert2("最大限额应该大于0！", true);
            return;
        }

        CMD.lottery_rate_ranges($(".f1 select").val(), {
            targets: "0," + (v * 100),
        }, function() {
            alert2("修改完成！");
        });
    });

    $(".btn-count-limit").click(function() {
        var v = $(".bet-count-max").val().trim();
        if (v == '') {
            alert2("请输入最大注单数，0表示不做限制！", true);
            return;
        }
        v = parseInt(v);
        if (v < 0) {
            alert2("最大注单数不能为负数！", true);
            return;
        }

        startLoading();
        Classify.save("bet.count", $(".f1 select").val(), v + '', function() {
            stopLoading();
            alert2("操作成功！");
        });
    });
</script>
